package com.yl.ylnews.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yl.ylnews.MainActivity;
import com.yl.ylnews.R;
import com.yl.ylnews.base.imp.NewsPager;
import com.yl.ylnews.global.GlobalConstants;

/**
 * Created by Administrator on 2016/10/17 0017.
 */

public class LeftMenuFragment extends BaseFragment{


    private ListView lv_menu_list;
    private int currentPosition;
    private MenuAdapter menuAdapter;

    @Override
    public void initData() {
        menuAdapter = new MenuAdapter();
        lv_menu_list.setAdapter(menuAdapter);
        lv_menu_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentPosition = position;
                menuAdapter.notifyDataSetChanged();
                toggle();
                setCurrentMenuPager(position);

            }
        });
    }

    private void setCurrentMenuPager(int position) {
        MainActivity maiUI = (MainActivity) mActivity;
        MainFragment contentFragment = maiUI.getContentFragment();
        NewsPager newsPager = contentFragment.getNewsPager();
        newsPager.setCurrentMenuDetailPager(position);
    }

    private void toggle() {
        MainActivity maiUI = (MainActivity) mActivity;
        maiUI.getSlidingMenu().toggle();
    }

    @Override
    public View initView() {
        View leftMenu = View.inflate(getActivity(), R.layout.fragment_left_menu, null);
        lv_menu_list = (ListView) leftMenu.findViewById(R.id.lv_menu_list);

        return leftMenu;
    }





    class MenuAdapter extends BaseAdapter {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(mActivity,R.layout.item_left_menu,null);
            TextView tv_menu_item = (TextView) convertView.findViewById(R.id.tv_menu_item);
            tv_menu_item.setText(GlobalConstants.MENU_ARR[position]);
            if (position == currentPosition) {
                tv_menu_item.setEnabled(false);
            } else {
                tv_menu_item.setEnabled(true);
            }

            return convertView;
        }

        @Override
        public int getCount() {
            return GlobalConstants.MENU_ARR.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }


    }



}
